package com.iMANIT.DS.DubLinkedList
{
	public final class DLLnodePool
	{
		private var _length:int;
		private var _head:DLLnode;
		private var _tail:DLLnode;
		
		public function DLLnodePool()
		{
			_length = 0;
		}
		
		public function borrowNode(payload:*):DLLnode
		{
			_length--;
			
			var node:DLLnode = _tail;
			_tail = _tail.forward;
			
			node.payload = payload;
			node.forward = null;
			node.backward = null;
			
			return node;
		}
		
		public function returnNode(node:DLLnode):void
		{
			node.payload = null;
			node.forward = null;
			
			if (length == 0)
			{
				_head = _tail = node;
				node.backward = null
			
			}
			else
			{
				_head.forward = node;
				_head = node;
				node.backward = _head;
			}
			_length++;
			
		}
		
		public function get length():int
		{
			return _length;
		}
	}
}